import 'dart:convert';
import 'dart:typed_data';
import 'package:fast_gbk/fast_gbk.dart';

/*
https://blog.51cto.com/u_15955464/6041375:

string.startWith('http')

依赖gbk: fast_gbk: ^1.0.0
https://pub.flutter-io.cn/packages/fast_gbk/install
import 'package:fast_gbk/fast_gbk.dart';


主要: str=str.trim() 还会去除\n等
 */
abstract class XStringUtils{

  static String dynamicToString(dynamic data){
    return '$data';
  }

  //字符串逐个字符转list
  static List<String> string2List(String? str){
    if(str==null){
      return [];
    }
    return str.split('');
  }

  static String list2String(List<dynamic> list,{String separator=''}){
    return list.join(separator);
  }

  static bool isEmpty(String? str){
    return str==null || str.trim().isEmpty;
  }

  //比较字符串(不区分大小写)
  static bool equalsIgnoreCase(String? a,String? b){
    return a?.toLowerCase() == b?.toLowerCase();
  }

  //检测是否以某个字符开头
  static bool startWith(String text,String start){
    return text.startsWith(RegExp(start));
  }



  //bytes to string
  static String uint8ListToString_utf8(
      List<int> data,
      {
        bool? allowMalformed
      }){
    return utf8.decode(data,allowMalformed:allowMalformed);
  }

  static String uint8ListToString_gbk(List<int> data,{bool? allowMalformed}){
    return gbk.decode(data,allowMalformed:allowMalformed);
  }


  //Uint8List转String utf16
  static String uint8ListToString_utf16(List<int> data){
    return String.fromCharCodes(data);
  }

  //数组中存在超过byte范围的数值(>128),需要转成对于byte范围内(128),
  static String intToString(List<int> intArray,{String type='gbk'}) {
    // 创建Uint8List自动处理范围
    Uint8List byteList = Uint8List.fromList(
        intArray.map((v) => v & 0xFF).toList()
    );

    if(type=='gbk'){
      return uint8ListToString_gbk(intArray);
    }

    return uint8ListToString_utf8(intArray);
  }

  /*
  注意:android的byte是8位的(-128~127), flutter没有byte只有int,因此没有负数, 在flutter的List<int>中,>=128的数,减256后即为对应java的byte数据,
因此base64转List<int>或String转转List<int>后的数据值是没有问题的,可直接写入
   */

  /*
  读:
  File gbkFile = File("gbkFile.txt");
  var stream = gbkFile.openRead();
  stream.transform(gbk.decoder)
      .transform(const LineSplitter())
      .listen((line) {
    stdout.writeln(line);
  });

  写:
  File output = File("gbk.txt");
  var stream = output.openWrite(encoding: gbk);
  stream.write("123");
  stream.writeln("456");
  stream.writeCharCode(0x41);
  await stream.close();
   */

  //string to bytes
  static List<int> string2Byte_utf8(String str){
    var list = utf8.encode(str);
    return list;
  }

  //String转Uint8List utf8
  static Uint8List stringToUint8List_utf8(String text){
    return Uint8List.fromList(utf8.encode(text));
  }

  //String转Uint8List gbk
  static Uint8List stringToUint8List_gbk(String text){
    return Uint8List.fromList(gbk.encode(text));
  }

  //String转Uint8List utf16
  static Uint8List stringToUint8List_utf16(String text){
    return Uint8List.fromList(text.codeUnits);
  }


  ///////////////////////////////

  //转小写
  static String toLowerCase(String str){
    return str.toLowerCase();
  }

  static String delHtmlTag(String htmlString){
    String plainText = htmlString.replaceAll(RegExp(r'<[^>]*>'), '');
    return plainText;
  }


}
